package io.io;

import java.io.*;

import org.junit.Test;

/**
 * 演示字符流
 * 	1.字符流是以字符为单位进行读取和写入的，用于【文本】的读写
 * 	2.字符流的根流
 * 		输出流：Writer
 * 		输入流：Reader
 * @author Administrator
 *
 */
public class CharIODemo {
	
	/**
	 * 使用文件字符流
	 * @throws IOException 
	 */
	@Test
	public void demo() throws IOException {
		
		// 创建流
		FileReader reader = new FileReader("/Users/wangzhipeng/Desktop/a.txt");
		
		// 读取
		int content = reader.read();
//		while(content != -1) {
//			System.out.print((char)content);
//			content = reader.read();
//		}
		
		char[] content1 = new char[1024];
		int length = reader.read(content1);
		while(length != -1) {
			System.out.println(new String(content1));
			length = reader.read(content1);
		}
		
//		reader.read(cbuf, offset, length);
		
		// 关闭流
		reader.close();
	}
	
	/**
	 * 字符输出流
	 * @throws IOException 
	 */
	@Test
	public void demo2() throws IOException {
		// 创建字符输出流
		FileWriter writer = new FileWriter("/Users/wangzhipeng/Desktop/b.txt");

		
		// 
		writer.write(65);
		
//		writer.write(char[]);
		
//		writer.write(cbuf, off, len); // 比较常用
		
		writer.write("xxxxxxxxxxx"); // 写字符串
		System.out.println("写入完毕");
		// 关闭
		writer.close();

		System.out.println("读取：");
		FileReader fileReader = new FileReader("/Users/wangzhipeng/Desktop/b.txt");
		char[] c = new char[1024];
		int len = fileReader.read(c);
		while(len!=-1){
			System.out.println(new String(c));
			len = fileReader.read(c);
		}
		fileReader.close();

	}
	
	/**
	 * 复制：复制图片不行的
	 * @throws IOException 
	 */
	@Test
	public void demo3() throws IOException {
		// 创建输入输出流
		FileReader reader = new FileReader("F:\\1.jpg");
		FileWriter writer = new FileWriter("F:\\2.jpg");
		
		// 复制
		int content = reader.read();
		while(content != -1) {
			// 写出
			writer.write(content);
			// 继续读
			content = reader.read();
		}
		
		// 关闭资源
		reader.close();
		writer.close();
	}
	
	
	/**
	 * 复制：文本
	 * @throws IOException 
	 */
	@Test
	public void demo4() throws IOException {
		// 创建流
		FileWriter writer = new FileWriter("F:\\aaa.txt");
		FileReader reader = new FileReader("F:\\a.txt");
		
		// 复制
		int content = reader.read();
		while(content != -1) {
			// 写入
			writer.write(content);
			// 继续读
			content = reader.read();
		}
		
		// 关闭流
		writer.close();
		reader.close();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
